//
//  AccountTableViewCell.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/8.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "AccountTableViewCell.h"


@interface AccountTableViewCell ()
@property(nonatomic, strong) UIImageView * alipay;
@property(nonatomic, strong) UILabel * lab1;
@property(nonatomic, strong) UILabel * lab2;
@property(nonatomic, strong) UISwitch * newsSwitch;
@end


@implementation AccountTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}


- (void)initView{
    
    self.backgroundColor=[UIColor whiteColor];
    
    UIView * bg = [[UIView alloc]init];
    [self addSubview:bg];
    bg.backgroundColor = RGB(250, 250, 250);
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.mas_top).offset(15);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.right.equalTo(self.mas_right).offset(-15);
    }];
    
    UIImageView * alipay = [[UIImageView alloc]init];
    [bg addSubview:alipay];
    alipay.image = [UIImage imageNamed:@""];
    [alipay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bg.mas_left).offset(15);
        make.top.equalTo(bg.mas_top).offset(15);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(18);
    }];
    _alipay=alipay;
    
    UILabel * lab1 = [[UILabel alloc]init];
    [bg addSubview:lab1];
    [self setLab:lab1 withColor:RGB(140, 159, 173) andFont:15 andText:@"加载中" andTextAlignment:NSTextAlignmentLeft];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(alipay.mas_right).offset(15);
        make.centerY.equalTo(alipay.mas_centerY).offset(-5);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(300);
    }];
    _lab1=lab1;
    
    UILabel * lab2 = [[UILabel alloc]init];
    [bg addSubview:lab2];
    [self setLab:lab2 withColor:RGB(30, 63, 89) andFont:15 andText:@"加载中" andTextAlignment:NSTextAlignmentLeft];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lab1.mas_left).offset(0);
        make.bottom.equalTo(bg.mas_bottom).offset(-20);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(300);
    }];
    _lab2=lab2;
    
    
    UIButton * btn = [[UIButton alloc]init];
    [bg addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bg.mas_right).offset(0);
        make.bottom.top.equalTo(bg).offset(0);
        make.width.mas_equalTo(100);
    }];
    btn.backgroundColor=[UIColor clearColor];
    
    UISwitch * newsSwitch = [[UISwitch alloc]init];
    
    // 开关开启时的颜色（默认绿色）
//    newsSwitch.onTintColor= [UIColor redColor];
    
    // 开关圆圈的颜色（默认白色）
//    newsSwitch.thumbTintColor= [UIColor redColor];
    
    // 开关关闭时的边框颜色（默认白色）
//    newsSwitch.tintColor= [UIColor redColor];
    
    [bg addSubview:newsSwitch];
    [newsSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bg.mas_right).offset(-15);
        make.centerY.equalTo(bg.mas_centerY);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(60);
    }];
    [newsSwitch addTarget:self action:@selector(switchChange:)forControlEvents:UIControlEventValueChanged];
    
    
    _newsSwitch=newsSwitch;

    
    
}

- (void) switchChange:(UISwitch*)sender {
    
    if([sender isOn]){
        
        if (self.block) {
            self.block(_dataSource,YES);
        }
        
        NSLog(@"turn on");
        
    }else{
        if (self.block) {
            self.block(_dataSource,NO);
        }
        NSLog(@"turned off");
        
    }
    
}


-(void)setDataSource:(AccountMinePaymentModel *)dataSource{
    _dataSource = dataSource;
    if (!dataSource) {
        return;
    }
    if ([dataSource.symbol containsString:@"支付宝"]) {
        _alipay.image = [UIImage imageNamed:@"支付宝"];
    }else if ([dataSource.symbol containsString:@"微信"]) {
        _alipay.image = [UIImage imageNamed:@"微信-3"];
    }else if ([dataSource.symbol containsString:@"银行卡"]) {
        _alipay.image = [UIImage imageNamed:@"银行卡"];
    }
    
//        branch = "\U897f\U5b89";
//        "card_number" = 6222600810020865911;
//        "create_address" = "\U4e2d\U56fd\U94f6\U884c";
//        name = "\U738b\U51ef";

    NSDictionary * account = dataSource.account;
    if ([dataSource.symbol containsString:@"银行卡"]) {
        _lab1.text = [NSString stringWithFormat:@"%@",account[@"create_address"]];
        _lab2.text = [NSString stringWithFormat:@"%@",account[@"card_number"]];
    } else {
        _lab1.text= [NSString stringWithFormat:@"%@",dataSource.symbol];
        _lab2.text= [NSString stringWithFormat:@"%@",account[@"account"]];;
    }
    
    
   
    if ([dataSource.status integerValue]==1) {
        [_newsSwitch setOn:YES animated:NO];
    }else{
        [_newsSwitch setOn:NO animated:NO];
    }
    
}


+(CGFloat)computeHeight:(id)info{
    
    return 95;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
